/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.football.lottery.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author god
 */
public class OrderCombination
{
//  public static void main(String[] args)
//  {
//    List testData = new ArrayList(Arrays.asList(new Object[]
//        {
//          "1", "2", "3", "4", "5", "6"
//        }));
//    List results = getAllCombinations(testData);
//    for (int length = 0; length < results.size(); length++)
//    {
//      System.out.println(results.get(length));
//    }
//  }

  public static List getAllCombinations(String[] data)
  {
    return getAllCombinations(Arrays.asList(data));
  }

  public static List getAllCombinations(List data)
  {
    List allCombinations = new ArrayList();
    for (int length = 1; length <= data.size(); length++)
    {
      allCombinations.addAll(getCombinations(data, length));
    }

    return allCombinations;
  }

  public static List getCombinations(String[] data, int length)
  {
    return getCombinations(Arrays.asList(data), length);
  }

  public static List getCombinations(List data, int length)
  {
    List allCombinations = new ArrayList();
    List initialCombination = new ArrayList();
    combination(allCombinations, data, initialCombination, length);
    return allCombinations;
  }

  private static void combination(List allCombinations, List data, List initialCombination, int length)
  {
    if (length == 1)
    {
      for (int i = 0; i < data.size(); i++)
      {
        List newCombination = new ArrayList(initialCombination);
        newCombination.add(data.get(i));
        allCombinations.add(newCombination);
      }
    }

    if (length > 1)
    {
      for (int i = 0; i < data.size(); i++)
      {
        List newCombination = new ArrayList(initialCombination);
        newCombination.add(data.get(i));

        List newData = new ArrayList(data);
        for (int j = 0; j <= i; j++)
        {
          newData.remove(data.get(j));
        }
        combination(allCombinations, newData, newCombination, length - 1);
      }
    }
  }
}
